package org.glut.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.glut.backend.domain.*;
import org.glut.backend.dto.MovieSchedulingDTO;
import org.glut.backend.mapper.*;
import org.glut.backend.service.IMovieSchedulingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glut.backend.vo.ChooseSeatDetailVO;
import org.glut.backend.vo.MovieSchedulingInfoVO;
import org.glut.backend.vo.PayOrderInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 电影排片表 服务实现类
 *
 * @author xiuci1412
 * @since 2025-08-01 17:43
 */
@Service
public class MovieSchedulingServiceImpl extends ServiceImpl<MovieSchedulingMapper, MovieScheduling> implements IMovieSchedulingService {

    @Autowired
    private StreetFoodMapper streetFoodMapper;
    @Autowired
    private CinemaMapper cinemaMapper;
    @Autowired
    private FilmMapper filmMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public MovieSchedulingInfoVO getMovieSchedulingInfoVO(Integer cinemaId) {
        MovieSchedulingInfoVO movieSchedulingInfoVO = new MovieSchedulingInfoVO();
        Cinema cinema = cinemaMapper.selectById(cinemaId);
        movieSchedulingInfoVO.setCinemaId(cinemaId);
        movieSchedulingInfoVO.setCinemaName(cinema.getCinemaname());
        movieSchedulingInfoVO.setCinemaLocation(cinema.getCinemalocation());
        LambdaQueryWrapper<StreetFood> streetFoodLambdaQueryWrapper = new LambdaQueryWrapper<>();
        streetFoodLambdaQueryWrapper.eq(StreetFood::getCinemaId,cinemaId);
        List<Film> films = filmMapper.selectList(null);
        movieSchedulingInfoVO.setFilmList(films);
        List<StreetFood> streetFoods = streetFoodMapper.selectList(streetFoodLambdaQueryWrapper);
        Map<Integer, List<StreetFood>> streamFoodList = streetFoods.stream()
                .collect(Collectors.groupingBy(StreetFood::getType));
        movieSchedulingInfoVO.setStreetFoodList(streamFoodList);
        return movieSchedulingInfoVO;
    }

    @Override
    public List<MovieScheduling> getMovieScheduling(MovieSchedulingDTO movieSchedulingDTO) {
        Date schedulingDate = movieSchedulingDTO.getSchedulingDate();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        LambdaQueryWrapper<MovieScheduling> movieSchedulingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        movieSchedulingLambdaQueryWrapper.eq(MovieScheduling::getCinemaId,movieSchedulingDTO.getCinemaId());
        movieSchedulingLambdaQueryWrapper.eq(MovieScheduling::getMovieId,movieSchedulingDTO.getMovieId());
        movieSchedulingLambdaQueryWrapper.eq(MovieScheduling::getSliceArrangementDate,simpleDateFormat.format(schedulingDate));
        movieSchedulingLambdaQueryWrapper.gt(MovieScheduling::getStartTime,new Date());
        movieSchedulingLambdaQueryWrapper.orderByAsc(MovieScheduling::getStartTime);
        return baseMapper.selectList(movieSchedulingLambdaQueryWrapper);
    }

    @Override
    public ChooseSeatDetailVO getChooseSeat(Integer id) {
        ChooseSeatDetailVO chooseSeatDetailVO = new ChooseSeatDetailVO();
        MovieScheduling movieScheduling = baseMapper.selectById(id);
        Film film = filmMapper.selectById(movieScheduling.getMovieId());
        Cinema cinema = cinemaMapper.selectById(movieScheduling.getCinemaId());
        BeanUtils.copyProperties(movieScheduling,chooseSeatDetailVO);
        chooseSeatDetailVO.setMovieSchedulingId(id);
        chooseSeatDetailVO.setCinemaName(cinema.getCinemaname());
        chooseSeatDetailVO.setFilmName(film.getFilmname());
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getSchedulingId,id);
        ordersLambdaQueryWrapper.in(Orders::getStatus, Arrays.asList(0,1));
        List<Orders> orders = ordersMapper.selectList(ordersLambdaQueryWrapper);
        List<String> soldSeats = orders.stream().map(Orders::getSoldSeat).collect(Collectors.toList());
        List<String> soldSeatList = new ArrayList<>();
        soldSeats.forEach(item->{
            soldSeatList.addAll(Arrays.stream(item.split(",")).collect(Collectors.toList()));
        });
        chooseSeatDetailVO.setSoldList(soldSeatList);
        return chooseSeatDetailVO;
    }

    @Override
    public PayOrderInfoVO payOrder(Integer id) {
        PayOrderInfoVO payOrderInfoVO = new PayOrderInfoVO();
        MovieScheduling movieScheduling = baseMapper.selectById(id);
        Film film = filmMapper.selectById(movieScheduling.getMovieId());
        Cinema cinema = cinemaMapper.selectById(movieScheduling.getCinemaId());
        BeanUtils.copyProperties(movieScheduling,payOrderInfoVO);
        payOrderInfoVO.setMovieSchedulingId(id);
        payOrderInfoVO.setCinemaName(cinema.getCinemaname());
        payOrderInfoVO.setFilmName(film.getFilmname());
        payOrderInfoVO.setFilmImg(film.getFilmurl());
        LambdaQueryWrapper<StreetFood> streetFoodLambdaQueryWrapper = new LambdaQueryWrapper<>();
        streetFoodLambdaQueryWrapper.eq(StreetFood::getCinemaId,movieScheduling.getCinemaId());
        streetFoodLambdaQueryWrapper.eq(StreetFood::getType,1);
        List<StreetFood> streetFoods = streetFoodMapper.selectList(streetFoodLambdaQueryWrapper);
        payOrderInfoVO.setStreetFoodList(streetFoods);
        return payOrderInfoVO;
    }
}